<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class Productview extends MX_Controller
{

    private $_langCode;

    public function __construct()
    {
        parent::__construct();
        $this->load->library('DefaultLayout');
        $this->load->library('Minifier');
        $this->load->language('common', $this->_langCode);
        $this->_langCode = $this->mycommon->getLangCode();
    }

    public function index($id)
    {
        $data = array();
        $data['common_lang'] = $this->lang->line('default_lang_common');
        $this->load->model('productview/productview_model');
        $lang = $this->mycommon->fetchLangCode($this->_langCode);
        $parentCat = $this->productview_model->getCategory(0, $lang);
        $data['category'] = array();
        foreach ($parentCat as $v) {
            $data['category'][$v['id']]['name'] = $v['name'];
            $data['category'][$v['id']]['childCat'] = array();
            $tmpCat = $this->productview_model->getCategory($v['id'], $lang);
            foreach ($tmpCat as $k) {
                $data['category'][$v['id']]['childCat'][$k['id']]['name'] = $k['name'];
            }
        }
        $product = $this->productview_model->get_product_by_id($id, $lang);
		$parentCat = array();
		$parentCat[] = $this->productview_model->getCategoryById($product[0]['product_id'], $lang);
		$parentCat[] = $this->productview_model->getCategoryById($parentCat[0][0]['parent_id'], $lang);
		$parentCat = array_reverse($parentCat);
		$i = 0;
		foreach ($parentCat as $k => $v)
		{
			if($i == 0)
				$this->breadcrumb->addCrumb($v[0]['name']);
			else
				$this->breadcrumb->addCrumb($v[0]['name'], site_url('home/cat/'.$v[0]['id']));
			$i++;
		}
		$this->breadcrumb->addCrumb($product[0]['name']);
		$data['breadcrumb'] = $this->breadcrumb->makeBread();
        $data['item'] = $product[0];
        $cat = $this->productview_model->getProductIdById($id);
        foreach ($cat as $cat) {
            $catId = $cat['product_id'];
        }
        $langCode = $this->mycommon->fetchLangCode($this->_langCode);
        $allProduct = $this->productview_model->getProductByCatId($catId, $langCode);
        $data['list'] = $allProduct;
        $this->defaultlayout->_loadHeader();
        $this->load->view('productview/index_view', $data);
        $this->defaultlayout->_loadFooter();
    }

}

?>
